export const ACTION_SET_FROM = 'SET_FROM'
export const ACTION_SET_TO = 'SET_TO'
export const ACTION_SET_IS_CITY_SELECTOR_VISIBLE =
  'SET_IS_CITY_SELECTOR_VISIBLE'
export const ACTION_SET_CURRENT_SELECTING_LEFT_CITY =
  'SET_CURRENT_SELECTING_LEFT_CITY'
export const ACTION_SET_CITY_DATA = 'SET_CITY_DATA'
export const ACTION_SET_IS_LOADING_CITY_DATA = 'SET_IS_LOADING_CITY_DATA'
export const ACTION_SET_IS_DATE_SELECTOR_VISIBLE =
  'SET_IS_DATE_SELECTOR_VISIBLE'
export const ACTION_SET_HIGH_SPEED = 'SET_HIGH_SPEED'
export const ACTION_SET_DEPART_DATE = 'SET_DEPART_DATE'

export function setFrom(payload) {
  return {
    type: ACTION_SET_FROM,
    payload,
  }
}

export function setTo(payload) {
  return {
    type: ACTION_SET_TO,
    payload,
  }
}

export function setCityData(payload) {
  return {
    type: ACTION_SET_CITY_DATA,
    payload,
  }
}

export function setIsLoadingCityData(payload) {
  return {
    type: ACTION_SET_IS_LOADING_CITY_DATA,
    payload,
  }
}

export function toggleHighSpeed() {
  return (dispatch, getState) => {
    const { highSpeed } = getState()
    dispatch({
      type: ACTION_SET_HIGH_SPEED,
      payload: !highSpeed,
    })
  }
}

export function showCitySelector(currentSelectingLeftCity) {
  return dispatch => {
    dispatch({
      type: ACTION_SET_IS_CITY_SELECTOR_VISIBLE,
      payload: true,
    })

    dispatch({
      type: ACTION_SET_CURRENT_SELECTING_LEFT_CITY,
      payload: currentSelectingLeftCity,
    })
  }
}

export function hideCitySelector() {
  return {
    type: ACTION_SET_IS_CITY_SELECTOR_VISIBLE,
    payload: false,
  }
}

export function setSelectedCity(city) {
  return (dispatch, getState) => {
    const { currentSelectingLeftCity } = getState()

    if (currentSelectingLeftCity) {
      dispatch(setFrom(city))
    } else {
      dispatch(setTo(city))
    }

    dispatch(hideCitySelector())
  }
}

export function showDateSelector() {
  return {
    type: ACTION_SET_IS_DATE_SELECTOR_VISIBLE,
    payload: true,
  }
}

export function hideDateSelector() {
  return {
    type: ACTION_SET_IS_DATE_SELECTOR_VISIBLE,
    payload: false,
  }
}

export function exchangeFromTo() {
  return (dispatch, getState) => {
    const { from, to } = getState()

    dispatch(setFrom(to))
    dispatch(setTo(from))
  }
}

export function setDepartDate(departDate) {
  return {
    type: ACTION_SET_DEPART_DATE,
    payload: departDate,
  }
}

export function fetchCityData() {
  return (dispatch, getState) => {
    const { isLoadingCityData } = getState()
    if (isLoadingCityData) {
      return
    }
    dispatch(setIsLoadingCityData(true))

    // 从缓存读取
    const cache = JSON.parse(localStorage.getItem('city_data_cache') || '{}')
    if (Date.now() < cache.expires) {
      dispatch(setCityData(cache.data))
      dispatch(setIsLoadingCityData(false))
      return
    }

    fetch('/rest/cities?_' + Date.now())
      .then(res => res.json())
      .then(cityData => {
        dispatch(setCityData(cityData))

        // 放入缓存
        localStorage.setItem(
          'city_data_cache',
          JSON.stringify({
            expires: Date.now() + 60 * 1000,
            data: cityData,
          })
        )

        dispatch(setIsLoadingCityData(false))
      })
      .catch(err => {
        console.error(err)
        dispatch(setIsLoadingCityData(false))
      })
  }
}
